package com.tanhua.autocofig.template;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.tanhua.autocofig.properties.SmsProperties;

public class SmsTemplate {

    private SmsProperties smsProperties;

    public SmsTemplate(SmsProperties smsProperties) {
        this.smsProperties = smsProperties;
    }

    public void sendMessage(String mobile, String code){
        String AccessKeyId="LTAI5tPpxAwSYmSV87ibY5UC";
        String AccessKeySecret="hmKUCWE3lPwDZCzK0qAjDlpFppMyMD";
        try {
            Config config = new Config()
                    // 您的 AccessKey ID
                    .setAccessKeyId(smsProperties.getAccessKey())
                    // 您的 AccessKey Secret
                    .setAccessKeySecret(smsProperties.getSecret());
            // 访问的域名
            config.endpoint = "dysmsapi.aliyuncs.com";
            Client client = new com.aliyun.dysmsapi20170525.Client(config);
            ;

            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setSignName(smsProperties.getSignName())
                    .setTemplateCode(smsProperties.getTemplateCode())
                    .setPhoneNumbers(mobile)
                    .setTemplateParam("{\"code\":\"" + code + "\"}");
            //复制代码运行请自行打印API的返回值
            SendSmsResponse response = client.sendSms(sendSmsRequest);
            SendSmsResponseBody body = response.getBody();
            System.out.println(body.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
